<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <el-container>
      <!-- 左侧边栏 -->
      <el-aside :width="isCol ? '65px' : '200px'">
        <div class="toggle-button" @click="toggleShow">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened="true"
          :collapse="isCol"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="`${m.id}`" v-for="m in menulist" :key="m.id">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <i :class="iconsObj[m.id]"></i>
              <span>{{m.authName}}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item 
              :index="'/' + c.path" 
              v-for="c in m.children" 
              :key="c.id"
              @click="heightLight('/' + c.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{c.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 右侧主体内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data(){
    return {
      // 左侧菜单数据
      menulist:[],
      iconsObj:{
        125:'el-icon-user',
        103:'el-icon-shopping-bag-2',
        101:'el-icon-s-cooperation',
        102:'el-icon-s-operation',
        145:'el-icon-s-platform'
      },
      isCol: false,
      // 菜单高亮的地址
      activePath:''
    }
  },
  created(){
    this.getMenuList()
    // 页面更新时，根据地址所在让菜单高亮
    this.activePath = window.sessionStorage.getItem('index')
  }, 
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取菜单列表
    async getMenuList(){
      const {data:res} = await this.$http.get('menus')
      if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      //console.log(res);
    },
    // 点击切换菜单的折叠与展开
    toggleShow(){
      this.isCol = !this.isCol
    },
    // 点击存储状态，根据状态让对应的菜单实现高亮
    heightLight(path){
      window.sessionStorage.setItem('index',path)
      this.activePath = path
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;

  div {
    display: flex;
    align-items: center;

    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;

  .el-menu{
    border-right: none
}
}
.el-main {
  background-color: #eaedf1;
}
.toggle-button{
  background-color: #4a5064;
  font-size: 18px;
  line-height: 35px;
  text-align: center;
  color: #fff;
  letter-spacing: 2px;
  cursor: pointer;
}
</style>>

